
local Application = commonlib.inherit(commonlib.gettable("commonlib.ToolBase"));

function Application:ctor()
    _G.__on_init__ = commonlib.async_callback(function(...) self:OnInit(...) end);
    _G.__on_render__ = commonlib.async_callback(function(...) self:OnRender(...) end);
    _G.__on_exit__ = commonlib.async_callback(function(...) self:OnExit(...) end);
    _G.__on_mouse_down__ = commonlib.async_callback(function(...) self:OnMouseDown(...) end);
    _G.__on_mouse_move__ = commonlib.async_callback(function(...) self:OnMouseMove(...) end);
    _G.__on_mouse_up__ = commonlib.async_callback(function(...) self:OnMouseUp(...) end);
    _G.__on_mouse_wheel__ = commonlib.async_callback(function(...) self:OnMouseWheel(...) end);
    _G.__on_key_down__ = commonlib.async_callback(function(...) self:OnKeyDown(...) end);
    _G.__on_key_up__ = commonlib.async_callback(function(...) self:OnKeyUp(...) end);
    _G.__on_text_input__ = commonlib.async_callback(function(...) self:OnTextInput(...) end);
end

function Application:OnInit()
    self:TriggerEventCallBack("OnInit");
end

function Application:OnRender()
    self:TriggerEventCallBack("OnRender");
end

function Application:OnExit()
    self:TriggerEventCallBack("OnExit");
end

function Application:OnMouseDown(mouse_x, mouse_y, mouse_button)
    self:TriggerEventCallBack("OnMouseDown", mouse_x, mouse_y, mouse_button);
end

function Application:OnMouseMove(mouse_x, mouse_y, mouse_button)
    self:TriggerEventCallBack("OnMouseMove", mouse_x, mouse_y, mouse_button);
end

function Application:OnMouseUp(mouse_x, mouse_y, mouse_button)
    self:TriggerEventCallBack("OnMouseUp", mouse_x, mouse_y, mouse_button);
end

function Application:OnMouseWheel(mouse_x, mouse_y, wheel_delta)
    self:TriggerEventCallBack("OnMouseWheel", mouse_x, mouse_y, wheel_delta);
end

function Application:OnKeyDown(keycode)
    self:TriggerEventCallBack("OnKeyDown", keycode);
end

function Application:OnKeyUp(keycode)
    self:TriggerEventCallBack("OnKeyUp", keycode);
end

function Application:OnTextInput(text)
    self:TriggerEventCallBack("OnTextInput", text);
end

Application:InitSingleton();

commonlib.GetApplication = function()
    return Application;
end

-- Application:RegisterEventCallBack("OnInit", function()
--     commonlib.run(function()
--         commonlib.print("Application Init !!!");
--         -- if (commonlib.GetMainScriptPath() ~= "") then
--         --     commonlib.import(commonlib.GetMainScriptPath());
--         -- end
--     end);
-- end);
